package org.stars.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.stars.message.Message;
import org.stars.service.EthService;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import javax.validation.constraints.NotNull;

/**
 * @author 沧月
 * @date 2020/12/30
 * @desc 描述
 */

@RestController
@RequestMapping("/ethRinkeby")
@Slf4j
@Validated
public class EthController {

    @Autowired
    private EthService ethService;

    /**
     * @return 获取当前geth版本信息
     */
    @RequestMapping(value = "/findClientVersion")
    public Message<String> findClientVersion() {
        return ethService.findClientVersion();
    }

    /**
     * @return 创建钱包
     */
    @RequestMapping(value = "/newAccount")
    public Message<JSONObject> newAccount() {
        return ethService.newAccount();
    }


    /**
     * 根据地址查询eth余额
     *
     * @param address
     * @return
     */
    @RequestMapping(value = "/findEthBalance")
    public Message<String> findEthBalance(@NotNull String address) {
        return ethService.findEthBalance(address);
    }


    /**
     * ETH转账
     *
     * @return
     */
    @RequestMapping(value = "/transactionETH")
    public Message<String> ethTransaction(@NotNull String fromAddress, @NotNull String toAddress, @NotNull String value,
                                          @NotNull String gasLimit, @NotNull String privateKye) {
        return ethService.ethTransaction(fromAddress, toAddress, value, gasLimit, privateKye);
    }


    /**
     * ETH代币转账
     *
     * @return
     */
    @RequestMapping(value = "/transactionETHToken")
    public Message<String> transactionETHToken(@NotNull String fromAddress, @NotNull String toAddress, @NotNull String contractAddress, @NotNull String value,
                                               @NotNull String gasLimit, @NotNull String privateKye) {
        return ethService.ethTokenTransaction(fromAddress, toAddress, contractAddress, value, gasLimit, privateKye);
    }

    /**
     * 合同交易
     *
     * @return
     */
    @RequestMapping(value = "/contractTransaction")
    public Message<String> contractTransaction(@NotNull String contractAddress, @NotNull String toAddress,
                                               @NotNull String value, @NotNull String privateKye, @NotNull String gasLimit) {
        return ethService.contractTransaction(contractAddress, toAddress, value, privateKye, gasLimit);
    }


    /**
     * 合同部署
     *
     * @return
     */
    @RequestMapping(value = "/deployContract")
    public Message<String> deployContract(@NotNull String privateKye) {
        return ethService.deployContract(privateKye);
    }


    /**
     * 获取代币余额
     *
     * @return
     */
    @RequestMapping(value = "/eth20Balance")
    public Message<Integer> eth20Balance(@NotNull String address, @NotNull String contractAddress,
                                         @NotNull String privateKye, @NotNull String gasLimit) {
        return ethService.eth20Balance(address, contractAddress, privateKye, gasLimit);
    }


    /**
     * 设置代币余额
     *
     * @return
     */
    @RequestMapping(value = "/issue")
    public Message<String> issue(String contractAddress, String privateKye, String _recipient, String value, String gasLimit) {
        return ethService.issue(contractAddress, privateKye, _recipient, value, gasLimit);
    }
}
